package edu.ntu.im.p2ptrade.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ntu.im.p2ptrade.OwnerManager;
import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.action.helper.InitServlet;
import edu.ntu.im.p2ptrade.entity.User;

public class AddOrUpdateUser extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String user_name = request.getParameter("user_name");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		String photoURL = request.getParameter("photoURL");
		User user = new User();
		user.setName(user_name);
		user.setNickName(nickname);
		user.setPassword(password);
		user.setPhotoURL(photoURL);
		OwnerManager.saveOwner(user);
		if (!P2PTrade.isEstablished()){
			P2PTrade.init(OwnerManager.getOwner().getName(), InitServlet.configHome, InitServlet.port);
			P2PTrade.getInstance();
		}
		
		//request.setAttribute("user", user);
		//PrintWriter out = response.getWriter();
		//out.println("yes");
		// TODO Auto-generated method stub
		return mapping.findForward("success");
	}

}
